package com.lh.microservice2.common.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.client.RestTemplate;

/**
 * @author lh
 * @Description:
 * @date 2021/3/26 9:38
 */
@Configuration
public class MyRestTemplate {
	@Autowired
	private RestTemplateBuilder builder;


	@Primary //自动装配时当出现多个Bean候选者时，被注解为@Primary的Bean将作为首选者，否则将抛出异常
	@Bean
	public RestTemplate restTemplate() {
		return builder.build();
	}

	@Bean
	@LoadBalanced // 需要使用负载均衡,必须与Bean一同使用
	public RestTemplate ribbonRestTemplate() {
		return new RestTemplate();
	}
}
